package com.zys.controller;

import com.zys.pojo.User;
import com.zys.entity.Result;
import com.zys.entity.QueryInfo;
import com.zys.entity.PageResult;
import com.zys.service.UserService;
import com.zys.constant.MessageConstant;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 处理用户相关的操作
 */
@RestController
@RequestMapping("/user")
public class UserController {

  @Reference
  private UserService userService;

  /**
   * 根据条件获取用户列表
   *
   * @param queryInfo 查询条件
   * @return PageResult
   */
  @RequestMapping("/getUserList")
  public PageResult<User> getUserList(@RequestBody QueryInfo queryInfo) {
    try {
      return userService.getUserList(queryInfo);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * 根据 id 修改用户状态
   *
   * @param id 用户 id
   * @return Result
   */
  @RequestMapping("/changeState")
  public Result changeState(@RequestParam Integer id) {
    try {
      int i = userService.changeStateById(id);
      if (i > 0) {
        return new Result(true, MessageConstant.UPDATE_USER_STATE_SUCCESS);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return new Result(false, MessageConstant.UPDATE_USER_STATE_FAIL);
  }

  /**
   * 添加用户
   *
   * @param user 用户信息
   * @return Result
   */
  @RequestMapping("/add")
  public Result add(@RequestBody User user) {
    try {
      userService.add(user);
      return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.ADD_USER_FAIL);
    }
  }

  /**
   * 删除用户
   *
   * @param id 用户 id
   * @return Result
   */
  @RequestMapping("/delete")
  public Result delete(@RequestParam Integer id) {
    try {
      userService.deleteById(id);
      return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.DELETE_USER_FAIL);
    }
  }

  /**
   * 判断用户名是否已经存在
   *
   * @param username 用户名
   * @return Result
   */
  @RequestMapping("/exist")
  public Result exist(@RequestParam String username) {
    try {
      int exist = userService.exist(username);
      if (exist > 0) {
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return new Result(false, MessageConstant.GET_USERNAME_FAIL);
  }

  /**
   * 根据 id 获取用户信息
   *
   * @param id 用户 id
   * @return Result
   */
  @RequestMapping("/getUserById")
  public Result getUserById(@RequestParam Integer id) {
    try {
      return new Result(true, MessageConstant.GET_USER_SUCCESS, userService.findUserById(id));
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.GET_USER_FAIL);
    }
  }

  /**
   * 修改用户信息
   *
   * @param user 修改后的用户信息
   * @return Result
   */
  @RequestMapping("/edit")
  public Result edit(@RequestBody User user) {
    try {
      userService.edit(user);
      return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.EDIT_USER_FAIL);
    }
  }

  /**
   * 更换用户角色
   *
   * @param userId 用户 id
   * @param roleId 角色 id
   */
  @RequestMapping("/changeUserRole")
  public Result changeUserRole(@RequestParam("userId") Integer userId, @RequestParam("roleId") Integer roleId) {
    try {
      userService.changeUserRole(userId, roleId);
      return new Result(true, MessageConstant.CHANGE_USER_ROLE_SUCCESS);
    } catch (Exception e) {
      e.printStackTrace();
      return new Result(false, MessageConstant.CHANGE_USER_ROLE_FAIL);
    }
  }
}
